﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.TES.Entity.SystemEntity;
using ZDSoft.TES.Core;
using ZDSoft.TES.Service.SystemService;
using NHibernate.Criterion;
using ZDSoft.TES.Web.Helpers;
using ZDSoft.TES.Entity.FunctionEntity;
using ZDSoft.TES.Service.FunctionService;

namespace ZDSoft.TES.Web.Controllers
{
    public class EvaluateController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Update(int id)
        {
            return View();
        }
        [HttpPost]
        public void Update(int id, string term, string marks, string start, string end, string selected)
        {
            Evaluate entity = Container.Instance.Resolve<IEvaluateService>().FindByID(id);
            entity.Term = (Term)int.Parse(term);
            entity.FullMarks = int.Parse(marks);
            entity.StartTime = DateTime.Parse(start);
            entity.EndTime = DateTime.Parse(end);
            entity.Rules = null;
            entity.Rules = new List<Rule>();
            string[] ids = selected.Split(',');
            foreach (string i in ids)
            {
                entity.Rules.Add(Container.Instance.Resolve<IRuleService>().FindByID(int.Parse(i)));
            }
            Container.Instance.Resolve<IEvaluateService>().Update(entity);
        }
        public void Logout(int id)
        {
            Evaluate entity = Container.Instance.Resolve<IEvaluateService>().FindByID(id);
            if (entity.EvaluateSituation != EvaluateSituation.注销)
            {
                entity.EvaluateSituation = EvaluateSituation.注销;
            }
            else
            {
                DateTime now = DateTime.Now;
                if (entity.StartTime > now)
                {
                    entity.EvaluateSituation = EvaluateSituation.未开始;
                }
                else if (entity.StartTime <= now && entity.EndTime >= now)
                {
                    entity.EvaluateSituation = EvaluateSituation.评教中;
                }
                else
                {
                    entity.EvaluateSituation = EvaluateSituation.完成;
                }
            }
        }

        public JsonResult GetRulesByID(int id)
        {
            IList<Rule> rules = Container.Instance.Resolve<IEvaluateService>().FindByID(id).Rules;
            IList<Rule> full = Container.Instance.Resolve<IRuleService>().FindAll();
            IList<ZTreeHelper<Rule>.ZTreeNode> tree = new List<ZTreeHelper<Rule>.ZTreeNode>();
            foreach (Rule i in full)
            {
                ZTreeHelper<Rule>.ZTreeNode node = new ZTreeHelper<Rule>.ZTreeNode();
                node.id = i.ID;
                node.pid = 0;
                node.name = i.RuleName;
                if (rules.Contains(i))
                {
                    node.@checked = true;
                }
                else
                {
                    node.@checked = false;
                }
                tree.Add(node);
            }
            return this.Json(tree, JsonRequestBehavior.AllowGet);
        }

        public string GetEvaluateByID(int id)
        {
            Evaluate entity = Container.Instance.Resolve<IEvaluateService>().FindByID(id);
            string json = "{\"id\":\"" + entity.ID + "\",\"dep\":\"" + entity.Organization.OrganizationName + "\",\"term\":\"" + entity.Term + "\",\"marks\":\""+entity.FullMarks+"\",\"start\":\"" + entity.StartTime.ToShortDateString() + "\",\"end\":\"" + entity.EndTime.ToShortDateString() + "\",\"sit\":\"" + entity.EvaluateSituation + "\"}";
            return json;
        }

        public JsonResult GetOrganizations(int id, int pid, string name, int level)
        {
            //IList<ICriterion> conditions = new List<ICriterion>();
            //conditions.Add(Expression.Eq("Parent", null));
            //IList<Organization> organizations = Container.Instance.Resolve<IOrganizationService>().Find(conditions);
            IList<Organization> organizationAll = Container.Instance.Resolve<IOrganizationService>().FindAll();
            IList<Organization> organizations = new List<Organization>();
            foreach (Organization entity in organizationAll)
            {
                if (entity.Parent == null)
                {
                    organizations.Add(entity);
                }
            }

            IList<ZTreeHelper<Organization>.ZTreeNode> treeNodes = new List<ZTreeHelper<Organization>.ZTreeNode>();
            foreach (Organization entity in organizations)
            {
                ZTreeHelper<Organization>.ZTreeNode node = new ZTreeHelper<Organization>.ZTreeNode();
                node.id = entity.ID;
                node.name = entity.OrganizationName;
                node.pid = id;
                node.tld = "org" + entity.ID;
                treeNodes.Add(node);
            }
            return this.Json(treeNodes, JsonRequestBehavior.AllowGet);
        }

        public JsonResult getRules()
        {
            IList<Rule> rules = Container.Instance.Resolve<IRuleService>().FindAll();
            IList<ZTreeHelper<Quota>.ZTreeNode> tree = new List<ZTreeHelper<Quota>.ZTreeNode>();
            foreach (Rule entity in rules)
            {
                ZTreeHelper<Quota>.ZTreeNode node = new ZTreeHelper<Quota>.ZTreeNode();
                node.id = entity.ID;
                node.tld = entity.Remark;
                node.pid = 0;
                node.name = entity.RuleName;
                tree.Add(node);
            }
            return this.Json(tree, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public void Create(string para, string selectedID)
        {
            string[] paras =para.Split(',');
            string depID = paras[0].ToString();
            Term term = (Term)int.Parse(paras[1].ToString());
            string marks = paras[2].ToString();
            DateTime start = DateTime.Parse(paras[3].ToString());
            DateTime end = DateTime.Parse(paras[4].ToString());
            string[] ids = selectedID.Split(',');
            Evaluate entity = new Evaluate();
            entity.Organization = Container.Instance.Resolve<IOrganizationService>().FindByID(int.Parse(depID));
            entity.Term = term;
            entity.FullMarks = int.Parse(marks);
            entity.StartTime = start;
            entity.EvaluateSituation = EvaluateSituation.未开始;
            entity.EndTime = end;
            entity.Rules = new List<Rule>();
            foreach (string i in ids)
            {
                if (i != "1")
                {
                    entity.Rules.Add(Container.Instance.Resolve<IRuleService>().FindByID(int.Parse(i)));
                }
            }
            Container.Instance.Resolve<IEvaluateService>().Create(entity);

        }

        public string GetPaged(int pageIndex, int pageSize, string strSearchProperty,
            string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }

            int count;
            int pageNum;

            IList<Evaluate> list = new List<Evaluate>();
            IList<Evaluate> Evaluates = Container.Instance.Resolve<IEvaluateService>().GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);

            if (Evaluates.Count != 0)
            {
                int tmpcountI = count / pageSize;
                float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
                if (temCountF > float.Parse(tmpcountI.ToString()))
                {
                    pageNum = tmpcountI + 1;
                }
                else
                {
                    pageNum = tmpcountI;
                }
                string result = "[";
                foreach (Evaluate entity in Evaluates)
                {
                    result += "{\"id\":\"" + entity.ID + "\",\"dep\":\"" + entity.Organization.OrganizationName + "\",\"term\":\"" + entity.Term + "\",\"start\":\"" + entity.StartTime.ToShortDateString() + "\",\"end\":\"" + entity.EndTime.ToShortDateString() + "\",\"sit\":\"" + entity.EvaluateSituation + "\"},";
                }
                result += "{\"count\":\"" + pageNum + "\"}";
                result += "]";
                return result;
            }
            else
            {
                return null;
            }
        }
    }
}
